package com.onLineCar.management.utils;

import org.apache.batik.transcoder.TranscoderException;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.ImageTranscoder;
import org.apache.batik.transcoder.image.PNGTranscoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class DocxToImageConverter {
    public static BufferedImage convertDocxToImage(File docxFile) throws IOException {
        // 读取 DOCX 文件
        FileInputStream fis = new FileInputStream(docxFile);
        XWPFDocument document = new XWPFDocument(fis);

        // 创建图片转换器
        ImageTranscoder transcoder = new PNGTranscoder();

        // 设置转换器的输出参数
        TranscoderOutput transcoderOutput = new TranscoderOutput(new ByteArrayOutputStream());

        // 获取文档中的段落和文字内容
        for (XWPFParagraph paragraph : document.getParagraphs()) {
            for (XWPFRun run : paragraph.getRuns()) {
                String text = run.getText(0);
                if (text != null && !text.isEmpty()) {
                    // 创建绘图环境
                    BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
                    Graphics2D graphics = image.createGraphics();

                    // 渲染文字
                    transcoder.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR, new Color(0, 0, 0, 0));
                    transcoder.addTranscodingHint(ImageTranscoder.KEY_FORCE_TRANSPARENT_WHITE, Boolean.TRUE);
                    try {
                        transcoder.transcode(new TranscoderInput(new StringReader(text)), transcoderOutput);
                    } catch (TranscoderException e) {
                        throw new RuntimeException(e);
                    }

                    // 关闭绘图环境
                    graphics.dispose();

                    // 获取转换后的图片
                    ByteArrayOutputStream byteArrayOutputStream = (ByteArrayOutputStream) transcoderOutput.getOutputStream();
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
                    return ImageIO.read(byteArrayInputStream);
                }
            }
        }

        return null;
    }

    public static void main(String[] args) {
        try {
            String ss = "C:\\Users\\Administrator\\Desktop\\Contract.docx";
            File docxFile = new File(ss);
            BufferedImage image = convertDocxToImage(docxFile);
            if (image != null) {
                File outputFile = new File("output.png");
                ImageIO.write(image, "png", outputFile);
                System.out.println("Conversion complete. Output file: " + outputFile.getAbsolutePath());
            } else {
                System.out.println("No text found in the document.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
